package com.aob.tools;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Random;

/**
 * 字符串加密类
 */
public class SecurityUtil {

    /**
     * md5 1次加密
     */
    public static String md5Hex(String value) {
        return DigestUtils.md5Hex(value);
    }

    /**
     * 3次加密
     *
     * @param value
     * @return
     */
    public static String md5Hex3(String value) {
        for (int i = 0; i < 3; i++) {
            value = DigestUtils.md5Hex(value);
        }
        return value;
    }

    /**
     * 256 位加密
     *
     * @param value
     * @return
     */
    public static String sha256Hex(String value) {
        return DigestUtils.sha256Hex(value);
    }

    /**
     * 512 位加密
     *
     * @param value
     * @return
     */
    public static String sha512Hex(String value) {
        return DigestUtils.sha512Hex(value);
    }

    public static String getRandomString(int length) { //length表示生成字符串的长度  
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for(int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
}
